package com.lpf.generate;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;

public class CodeGenerate {

    public static void main(String[] args) throws IOException, TemplateException {

        String ftlDir = "E:\\IdeaProjects\\java-learning\\07-java-python\\src\\main\\resources\\ftl";
        String pythonMainFtl = "python_main.ftl";

        String rawCodeFilePath = "E:\\IdeaProjects\\java-learning\\07-java-python\\src\\main\\resources\\apps\\md5\\main\\run.py";
        String generateCodeFilePath = "E:\\IdeaProjects\\java-learning\\07-java-python\\src\\main\\resources\\apps\\md5\\main\\run_0.py";

        Configuration configuration = new Configuration(Configuration.getVersion());
        configuration.setDirectoryForTemplateLoading(new File(ftlDir));
        Template template = configuration.getTemplate(pythonMainFtl);

        HashMap<String, Object> data = new HashMap<>();

        ArrayList<MethodInfo> methodInfos = new ArrayList<>();
        methodInfos.add(MethodInfo.builder().methodName("encryption").args(ListUtil.toList("text", "type")).build());

        data.put("methods", methodInfos);
        data.put("code", FileUtil.readString(rawCodeFilePath, StandardCharsets.UTF_8));

        FileWriter fileWriter = new FileWriter(generateCodeFilePath);
        template.process(data, fileWriter);

        StringWriter stringWriter = new StringWriter();
        template.process(data, stringWriter);
        String resultStr = stringWriter.toString();
        System.out.println(resultStr);
    }
}
